#!/bin/bash

# 加载环境变量
source ./build_env.sh

# 设置目录
INSTALL_DIR=$BUILD_DIR/openssl
FRAMEWORK_DIR="$BUILD_DIR/framework"
FRAMEWORK_NAME="openssl"
FRAMEWORK_PATH="$FRAMEWORK_DIR/$FRAMEWORK_NAME.framework"

# 确保universal库已经构建
if [ ! -f "$INSTALL_DIR/universal/lib/libssl.a" ] || [ ! -f "$INSTALL_DIR/universal/lib/libcrypto.a" ]; then
    echo "错误：通用库不存在，请先运行build_openssl_static.sh"
    exit 1
fi

# 创建framework目录结构
rm -rf "$FRAMEWORK_PATH"
mkdir -p "$FRAMEWORK_PATH/Headers"
mkdir -p "$FRAMEWORK_PATH/Modules"

# 复制头文件
cp -R "$INSTALL_DIR/universal/include/openssl" "$FRAMEWORK_PATH/Headers/"

# 创建合并的库文件 - OpenSSL有两个主要库：libssl和libcrypto
# 我们需要将它们合并为一个库文件
echo "正在合并libssl.a和libcrypto.a为一个库文件..."

# 为每个架构分别处理
for ARCH in "${IOS_ARCHS[@]}"; do
    echo "处理 $ARCH 架构..."
    
    # 创建临时目录
    TMP_DIR="$(mktemp -d)"
    cd "$TMP_DIR"
    
    # 从fat库中提取特定架构的库
    lipo "$INSTALL_DIR/universal/lib/libssl.a" -thin $ARCH -output libssl_$ARCH.a
    lipo "$INSTALL_DIR/universal/lib/libcrypto.a" -thin $ARCH -output libcrypto_$ARCH.a
    
    # 解压架构特定的库
    mkdir -p ssl_objs crypto_objs
    cd ssl_objs
    ar -x "$TMP_DIR/libssl_$ARCH.a"
    cd ../crypto_objs
    ar -x "$TMP_DIR/libcrypto_$ARCH.a"
    cd ..
    
    # 创建合并的架构特定库
    ar -rc "$TMP_DIR/lib${FRAMEWORK_NAME}_$ARCH.a" ssl_objs/*.o crypto_objs/*.o
    ranlib "$TMP_DIR/lib${FRAMEWORK_NAME}_$ARCH.a"
    
    # 保存这个架构的库文件路径
    ARCH_LIBS+=( "$TMP_DIR/lib${FRAMEWORK_NAME}_$ARCH.a" )
    
    # 不要删除临时目录，我们稍后需要这些文件
    cd /
done

# 使用lipo合并所有架构的库为一个通用库
LIPO_ARGS=( )
for LIB in "${ARCH_LIBS[@]}"; do
    LIPO_ARGS+=( "$LIB" )
done

# 创建最终的framework二进制文件
lipo -create "${LIPO_ARGS[@]}" -output "$FRAMEWORK_PATH/$FRAMEWORK_NAME"

# 清理临时目录
for ARCH in "${IOS_ARCHS[@]}"; do
    rm -rf "$(dirname ${ARCH_LIBS[0]})"
done

# 创建Info.plist文件
cat > "$FRAMEWORK_PATH/Info.plist" << EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>CFBundleDevelopmentRegion</key>
    <string>en</string>
    <key>CFBundleExecutable</key>
    <string>$FRAMEWORK_NAME</string>
    <key>CFBundleIdentifier</key>
    <string>org.openssl</string>
    <key>CFBundleInfoDictionaryVersion</key>
    <string>6.0</string>
    <key>CFBundleName</key>
    <string>$FRAMEWORK_NAME</string>
    <key>CFBundlePackageType</key>
    <string>FMWK</string>
    <key>CFBundleShortVersionString</key>
    <string>3.0.7</string>
    <key>CFBundleVersion</key>
    <string>1</string>
    <key>CFBundleSupportedPlatforms</key>
    <array>
        <string>iPhoneOS</string>
        <string>iPhoneSimulator</string>
    </array>
    <key>MinimumOSVersion</key>
    <string>$IPHONEOS_DEPLOYMENT_TARGET</string>
</dict>
</plist>
EOF

# 创建模块映射文件
cat > "$FRAMEWORK_PATH/Modules/module.modulemap" << EOF
framework module openssl {
    umbrella header "openssl/opensslconf.h"
    export *
    module * { export * }
}
EOF

echo "openssl.framework 已成功创建在 $FRAMEWORK_PATH"
echo "您可以将此框架拖入您的Xcode项目中使用"